﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace Survivor
{
    public class ExpUpgradeSystem : AbstractSystem
    {
        public readonly List<ExpUpgradeItem> ExpUpgradeItemList = new List<ExpUpgradeItem>();
        public static EasyEvent OnExpUpgradeSystemChanged = new EasyEvent();
        
        protected override void OnInit()
        {
            ResetData();
            Global.Level.Register((level) =>
            {
                Roll();
            });
        }

        public void ResetData()
        {
            ExpUpgradeItemList.Clear();
            Add(new ExpUpgradeItem()
                .WithKey("simple_damage_up")
                .WithDescribe(lv => $"简单能力攻击力提升 lv.{lv}")
                .WithMaxLevel(10)
                .OnUpgrade((_, level) => Global.SimpleAbilityDamage.Value += 1)
            );
            
            Add(new ExpUpgradeItem()
                .WithKey("attack_duration_up")
                .WithDescribe(lv => $"简单能力攻击频率提升 lv.{lv}")
                .WithMaxLevel(3)
                .OnUpgrade((_, level) =>
                {
                    Global.SimpleAbilityDuration.Value *= 0.8f;
                })
            );
        }
        
        private ExpUpgradeItem Add(ExpUpgradeItem item)
        {
            ExpUpgradeItemList.Add(item);
            return item;
        }

        private void Roll()
        {
            foreach (var expUpgradeItem in ExpUpgradeItemList)
            {
                expUpgradeItem.Visible.Value = false;
            }
            var randItem = ExpUpgradeItemList.Where(item => !item.UpgradeFinish).ToList().GetRandomItem();
            if (randItem != null)
            {
                randItem.Visible.Value = true;
            }
        }
    }
}